//
//  ImageScanViewController.m
//  Mtime
//
//  Created by Mac on 15/8/10.
//  Copyright (c) 2015年 wxk. All rights reserved.
//

#import "ImageScanViewController.h"
#import "ImageScanCVCell.h"

@interface ImageScanViewController ()
{

    UICollectionView *_imageScanCV;

}
@end

@implementation ImageScanViewController


- (void)setImageDataList:(NSMutableArray *)imageDataList {

    _imageDataList = imageDataList;
    
    [_imageScanCV reloadData];
}


//接受点击图片的位置
- (void)setCurrentIndexPath:(NSIndexPath *)currentIndexPath {

    _currentIndexPath = currentIndexPath;
    
    

}

- (void)backAction {

    [self dismissViewControllerAnimated:YES completion:nil];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"大图浏览";
    
    self.view.backgroundColor = [UIColor grayColor];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];
    
    
    
    //1.初始化布局类UICollectionViewFlowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    //设置水平滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    //设置视图到四边的距离
//    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //设置最小间距
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    //2.初始化UICollectionView  并传入布局类
    _imageScanCV = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    
    //3.设置UICollectionView的代理
    _imageScanCV.delegate = self;
    _imageScanCV.dataSource = self;
    
    //打开分业效果
    _imageScanCV.pagingEnabled = YES;
    
    //4.注册单元格
    [_imageScanCV registerClass:[ImageScanCVCell class] forCellWithReuseIdentifier:@"imageScanCell"];
    
    //使_imageScanCV滚动到对应位置
    [_imageScanCV scrollToItemAtIndexPath:_currentIndexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:NO];
    
    //5.添加到父视图上
    [self.view addSubview:_imageScanCV];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {


    return _imageDataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    ImageScanCVCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"imageScanCell" forIndexPath:indexPath];
    
    cell.imageName = _imageDataList[indexPath.item];
    
    return cell;
}


//当单元格结束显示时调用
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {

    ImageScanCVCell *scancell = (ImageScanCVCell *)cell;
    
    
    //调用方法 使单元格里的scrollview回复状态
    [scancell bringScrollViewZoom];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
